unit Torneo15;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, pngimage, ExtCtrls, Menus, ExtDlgs, Printers, Randomizzatore2;

type
  TT15 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Salva1: TMenuItem;
    Stampa1: TMenuItem;
    SavePictureDialog1: TSavePictureDialog;
    Image1: TImage;
    Label1: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    Label3: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    procedure Label1Click(Sender: TObject);
    procedure Label3Click(Sender: TObject);
    procedure Label4Click(Sender: TObject);
    procedure Label5Click(Sender: TObject);
    procedure Label6Click(Sender: TObject);
    procedure Label7Click(Sender: TObject);
    procedure Label8Click(Sender: TObject);
    procedure Label9Click(Sender: TObject);
    procedure Label10Click(Sender: TObject);
    procedure Label11Click(Sender: TObject);
    procedure Label12Click(Sender: TObject);
    procedure Label13Click(Sender: TObject);
    procedure Label14Click(Sender: TObject);
    procedure Label15Click(Sender: TObject);
    procedure Label16Click(Sender: TObject);
    procedure Label3ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label4ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label5ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label6ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label7ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label8ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label9ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label10ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label11ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label12ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label13ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label14ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label15ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label16ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label1ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label18ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label24ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label21ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label22ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label23ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label25Click(Sender: TObject);
    procedure Label25ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label26ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label28ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label27ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label29ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label30ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label20ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Label19ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure Salva1Click(Sender: TObject);
    procedure Stampa1Click(Sender: TObject);
    procedure ScreenShot(x : integer; y : integer; Width : integer; Height : integer; bm : TBitMap);
    function GetActiveFormImage: TBitmap;
    procedure PrintActiveForm(poPrintScale: TPrintScale);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  T15: TT15;

implementation

{$R *.dfm}


procedure TT15.Label10Click(Sender: TObject);
begin
  Label10.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label10.Caption);
end;

procedure TT15.Label10ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label20.Caption := Label10.Caption;
end;


procedure TT15.Label11Click(Sender: TObject);
begin
  Label11.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label11.Caption);
end;

procedure TT15.Label11ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label21.Caption := Label11.Caption;
end;

procedure TT15.Label12Click(Sender: TObject);
begin
  Label12.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label12.Caption);
end;

procedure TT15.Label12ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label21.Caption := Label12.Caption;
end;

procedure TT15.Label13Click(Sender: TObject);
begin
  Label13.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label13.Caption);
end;

procedure TT15.Label13ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label22.Caption := Label13.Caption;
end;

procedure TT15.Label14Click(Sender: TObject);
begin
  Label14.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label14.Caption);
end;

procedure TT15.Label14ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label22.Caption := Label14.Caption;
end;

procedure TT15.Label15Click(Sender: TObject);
begin
  Label15.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label15.Caption);
end;

procedure TT15.Label15ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label23.Caption := Label15.Caption;
end;

procedure TT15.Label16Click(Sender: TObject);
begin
  Label16.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label16.Caption);
end;

procedure TT15.Label16ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label23.Caption := Label16.Caption;
end;

procedure TT15.Label18ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label25.Caption := Label18.Caption;
end;

procedure TT15.Label19ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label26.Caption := Label19.Caption;
end;

procedure TT15.Label1Click(Sender: TObject);
begin
  Label1.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label1.Caption);
end;

procedure TT15.Label1ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label25.Caption := Label1.Caption;
end;

procedure TT15.Label20ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label28.Caption := Label20.Caption;
end;

procedure TT15.Label21ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label28.Caption := Label21.Caption;
end;

procedure TT15.Label22ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label27.Caption := Label22.Caption;
end;

procedure TT15.Label23ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label27.Caption := Label23.Caption;
end;

procedure TT15.Label24ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label26.Caption := Label24.Caption;
end;

procedure TT15.Label25Click(Sender: TObject);
begin
Label29.Caption := Label25.Caption;
end;

procedure TT15.Label25ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label29.Caption := Label25.Caption;
end;

procedure TT15.Label26ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label29.Caption := Label26.Caption;
end;

procedure TT15.Label27ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label30.Caption := Label27.Caption;
end;

procedure TT15.Label28ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label30.Caption := Label28.Caption;
end;

procedure TT15.Label29ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label31.Caption := Label29.Caption;
end;

procedure TT15.Label30ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label31.Caption := Label30.Caption;
end;

procedure TT15.Label3Click(Sender: TObject);
begin
  Label3.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label3.Caption);
end;

procedure TT15.Label3ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
  Label18.Caption := Label3.Caption;
end;

procedure TT15.Label4Click(Sender: TObject);
begin
  Label4.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label4.Caption);
end;

procedure TT15.Label4ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
  Label18.Caption := Label4.Caption;
end;

procedure TT15.Label5Click(Sender: TObject);
begin
  Label5.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label5.Caption);
end;

procedure TT15.Label5ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label19.Caption := Label5.Caption;
end;

procedure TT15.Label6Click(Sender: TObject);
begin
  Label6.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label6.Caption);
end;

procedure TT15.Label6ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label19.Caption := Label6.Caption;
end;

procedure TT15.Label7Click(Sender: TObject);
begin
  Label7.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label7.Caption);
end;

procedure TT15.Label7ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label24.Caption := Label7.Caption;
end;

procedure TT15.Label8Click(Sender: TObject);
begin
  Label8.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label8.Caption);
end;

procedure TT15.Label8ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label24.Caption := Label8.Caption;
end;

procedure TT15.Label9Click(Sender: TObject);
begin
  Label9.Caption := InputBox('Modificare il nome del Partecipante', 'Nome: ', Label9.Caption);
end;

procedure TT15.Label9ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
Label20.Caption := Label9.Caption;
end;

procedure TT15.Salva1Click(Sender: TObject);
var
  XWidth, XHeight: longint;
begin
  if SavepictureDialog1.Execute then
  begin
    XWidth := T15.Width;
    XHeight := T15.Height;
    T15.AutoSize := True;
    GetFormImage.SaveToFile(SavepictureDialog1.FileName);
    T15.AutoSize := False;
    T15.Width := XWidth;
    T15.Height := XHeight;
    T15.Resize;
  end;

end;
procedure TT15.Stampa1Click(Sender: TObject);
var
  XWidth, XHeight: longint;
begin
    XWidth := Width;
    XHeight := Height;
    AutoScroll := False;
    AutoSize := True;
    PrintActiveForm(poPrintToFit);
    Width := XWidth;
    Height := XHeight;
    AutoSize := False;
    AutoScroll := True;


end;
 procedure TT15.ScreenShot(x : integer; y : integer; Width : integer; Height : integer; bm : TBitMap);
var
  dc: HDC; lpPal : PLOGPALETTE;
begin
{test width and height}
  if ((Width = 0) OR (Height = 0)) then exit;
  bm.Width := Width;
  bm.Height := Height;
{get the screen dc}
  dc := GetDc(0);
  if (dc = 0) then exit;
{do we have a palette device?}
  if (GetDeviceCaps(dc, RASTERCAPS) AND RC_PALETTE = RC_PALETTE) then
  begin
    {allocate memory for a logical palette}
    GetMem(lpPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
    {zero it out to be neat}
    FillChar(lpPal^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
    {fill in the palette version}
    lpPal^.palVersion := $300;
    {grab the system palette entries}
    lpPal^.palNumEntries :=GetSystemPaletteEntries(dc,0,256,lpPal^.palPalEntry);
    if (lpPal^.PalNumEntries <> 0) then
    begin
      {create the palette}
      bm.Palette := CreatePalette(lpPal^);
    end;
    FreeMem(lpPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
  end;
  {copy from the screen to the bitmap}
  BitBlt(bm.Canvas.Handle,0,0,Width,Height,Dc,x,y,SRCCOPY);
  {release the screen dc}
  ReleaseDc(0, dc);
end;

function TT15.GetActiveFormImage: TBitmap;
var
  Ofs: Integer;
  R: TRect;
begin
  Result := TBitmap.Create;
  try
    R.TopLeft := ClientToScreen(Point(0,0));
    R.BottomRight := ClientToScreen(Point(ClientWidth,ClientHeight));
    ScreenShot(R.Left, R.Top, R.Right-R.Left, R.Bottom-R.Top, result);
    {Result.Width := ClientWidth;
    Result.Height := ClientHeight;
    Result.Canvas.Brush := Brush;
    Result.Canvas.FillRect(ClientRect);
    Result.Canvas.Lock;
    try
      Ofs := -1;
      PaintTo(Result.Canvas.Handle, Ofs, Ofs);
    finally
      Result.Canvas.Unlock;
    end;}
  except
    Result.Free;
    raise;
  end;
end;

procedure TT15.PrintActiveForm(poPrintScale: TPrintScale);
var
  FormImage: TBitmap;
  Info: PBitmapInfo;
  InfoSize: DWORD;
  Image: Pointer;
  ImageSize: DWORD;
  Bits: HBITMAP;
  DIBWidth, DIBHeight: Longint;
  PrintWidth, PrintHeight: Longint;
begin
  Printer.PrinterIndex := Form1.ComboBox1.ItemIndex;
  Printer.Orientation := poLandscape;
  Printer.BeginDoc;
  try
    FormImage := GetActiveFormImage;
    Canvas.Lock;
    try
      { Paint bitmap to the printer }
      with Printer, Canvas do
      begin
        Bits := FormImage.Handle;
        GetDIBSizes(Bits, InfoSize, ImageSize);
        Info := AllocMem(InfoSize);
        try
          Image := AllocMem(ImageSize);
          try
            GetDIB(Bits, 0, Info^, Image^);
            with Info^.bmiHeader do
            begin
              DIBWidth := biWidth;
              DIBHeight := biHeight;
            end;
            case poPrintScale of
              poProportional:
                begin
                  PrintWidth := MulDiv(DIBWidth, GetDeviceCaps(Handle,
                    LOGPIXELSX), PixelsPerInch);
                  PrintHeight := MulDiv(DIBHeight, GetDeviceCaps(Handle,
                    LOGPIXELSY), PixelsPerInch);
                end;
              poPrintToFit:
                begin
                  PrintWidth := MulDiv(DIBWidth, PageHeight, DIBHeight);
                  if PrintWidth < PageWidth then
                    PrintHeight := PageHeight
                  else
                  begin
                    PrintWidth := PageWidth;
                    PrintHeight := MulDiv(DIBHeight, PageWidth, DIBWidth);
                  end;
                end;
            else
              PrintWidth := DIBWidth;
              PrintHeight := DIBHeight;
            end;
            StretchDIBits(Canvas.Handle, 200, 200, PrintWidth-200, PrintHeight-200, 0, 0,
              DIBWidth, DIBHeight, Image, Info^, DIB_RGB_COLORS, SRCCOPY);
          finally
            FreeMem(Image, ImageSize);
          end;
        finally
          FreeMem(Info, InfoSize);
        end;
      end;
    finally
      Canvas.Unlock;
      FormImage.Free;
    end;
  finally
    Printer.EndDoc;
  end;
end;

end.
